//
//  VideosViewController.m
//  SYiDaiy
//
//  Created by dllo on 16/8/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "VideosViewController.h"
#import "TAMacro.h"
#import "VideoCustomCollectionViewCell.h"
#import "AFNetworking.h"
#import "VideosESRootClass.h"
#import "Videosesarray.h"
#import "TopVideosCollectionViewCell.h"
#import "VideosDetailViewController.h"
#import "MJRefresh.h"
@interface VideosViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) VideosESRootClass *videosESRootClass;
@property (nonatomic,strong) NSMutableArray<Videosesarray *> *videosArray;




@end

@implementation VideosViewController

#pragma mark -----视图加载
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"WATCHVIDEOS";
    [self creatCollectionView];
}



    
    




- (void)creatCollectionView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];

    flow.minimumInteritemSpacing = 5;
    flow.minimumLineSpacing = 5;
    flow.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64) collectionViewLayout:flow];
    [self.collectionView reloadData];
    [self networkGet];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[VideoCustomCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [_collectionView registerClass:[TopVideosCollectionViewCell class] forCellWithReuseIdentifier:@"topCell"];
    [self.view addSubview:_collectionView];
}


#pragma mark -----视频页网络请求
- (void)networkGet
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *requestURLString = @"http://watch-cdn.idailywatch.com/api/list/video/zh-hans?page=1&ver=iphone&app_ver=15";
    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        _videosArray = [NSMutableArray array];
        for (NSDictionary *dic in responseObject) {
            Videosesarray *videosesarray = [Videosesarray modelWithDic:dic];
            [_videosArray addObject:videosesarray];
        }
        [_collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}

#pragma mark ----collection返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
    TopVideosCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"topCell" forIndexPath:indexPath];
        [cell.cover_thumb_hdImageView sd_setImageWithURL:[NSURL URLWithString:_videosArray[0].cover_thumb_hd]];
        cell.titleLabel.text = _videosArray[indexPath.row].title;
        return cell;
    
    }
    VideoCustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell.cover_thumb_hdImageView sd_setImageWithURL:[NSURL URLWithString:_videosArray[indexPath.row + 1].cover_thumb_hd]];
    cell.titleLabel.text = _videosArray[indexPath.row + 1].title;
    cell.pubdateLabel.text = _videosArray[indexPath.row + 1].pubdate;
    return cell;
}

#pragma mark ----collection的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    } else {
    return 20;
}
}
#pragma mark ----返回collection的分区个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
#pragma mark ------返回collection的item的宽和高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH * 250 / 375);
    }
    CGFloat cellWidth = (SCREEN_WIDTH - 15) / 2;
    return CGSizeMake(cellWidth, cellWidth);
}
#pragma mark ----collection的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    VideosDetailViewController *videosDetailVC = [VideosDetailViewController new];
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    if (section == 0) {
        videosDetailVC.rowValue = row;
    } else {
    videosDetailVC.rowValue = row + 1;
    }
    [self.navigationController pushViewController:videosDetailVC animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
